package com.byit.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;


public class InspectionSign {

    public static boolean InspectionSign(String s1) throws UnsupportedEncodingException {
        Map mapTypes = JSON.parseObject(s1);
        Map<String, Object> data = InspectionSign.translateResultData(mapTypes);
        String checksign = String.valueOf(mapTypes.get("sign"));
        String checkContent = HttpClientPost.assembelSignaturingData(data);
        System.out.println("checkContent:" + checkContent);
        X509Certificate x509Certificate = null;
        Signature signatureCheck = null;
        try {
            //           InputStream pubStream = new FileInputStream(new File("/D:\\/天翼电子商务有限公司.cer"));
            InputStream pubStream = InspectionSign.class.getClassLoader().getResourceAsStream("天翼电子商务有限公司.cer");
            byte pubByte[] = new byte[2048];
            pubStream.read(pubByte);
            pubStream.close();
            x509Certificate = CryptoUtil.base64StrToCert(Base64Encrypt.getBASE64ForByte(pubByte));
            BouncyCastleProvider bouncyCastleProvider2 = new BouncyCastleProvider();
            signatureCheck = Signature.getInstance("SHA1withRSA", bouncyCastleProvider2);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        boolean isOk = SignatureUtil.verify(signatureCheck, checkContent, checksign, x509Certificate.getPublicKey());
        System.out.println(isOk);
        return isOk;
    }

    public static Map<String, Object> translateResultData(Map<String, Object> res) {
        Map<String, Object> resultDataMap = new TreeMap<String, Object>();
        Set set = res.entrySet();
        for (Iterator iter = set.iterator(); iter.hasNext(); ) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String) entry.getKey();
            Object value = entry.getValue();
//            if (!(value instanceof String)) {
            if (value instanceof String[]
                    || value instanceof Map
                //        || value instanceof java.util.List
                    ) {
                // 将数据转换成json字符串参与签名

                resultDataMap.put(key, JSON.toJSONString(value, SerializerFeature.WriteMapNullValue, SerializerFeature.UseISO8601DateFormat));
            } else {
                resultDataMap.put(key, value);
//                }
//            }else{
//                resultDataMap.put(key,value);
            }
        }
        return resultDataMap;
    }

    public static KeyCertInfo getKey() {

        InputStream resourceAsStream = InspectionSign.class.getClassLoader().getResourceAsStream("江油鸿飞大数据服务有限公司.p12");
        final String passwd = "68803441";
        String alias = "conname";
        String keyStoreType = "PKCS12";

        KeyCertInfo keyCertInfo = CryptoUtil.fileStreamToKeyCertInfo(resourceAsStream, passwd, keyStoreType, alias);
        return keyCertInfo;
    }

    public static Signature getSignture() {

        BouncyCastleProvider bouncyCastleProvider = new BouncyCastleProvider();
        Signature signature = null;
        try {
            signature = Signature.getInstance("SHA256withRSA", bouncyCastleProvider);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return signature;

    }
}
